package com.sys.object.common.page;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * @author csm
 * @createDate 2010-07-19
 * @description 分页标签
 */
public class PageTag extends SimpleTagSupport {

    /**
     * url路径要带问号的，有参数的要带&号的
     */
    private String url;
    private PageBean pageBean;
    private int tagnum;

    /**
     * @return the tagnum
     */
    public int getTagnum() {
        return tagnum;
    }

    /**
     * @param tagnum the tagnum to set
     */
    public void setTagnum(int tagnum) {
        this.tagnum = tagnum;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public PageBean getPageBean() {
        return pageBean;
    }

    public void setPageBean(PageBean pageBean) {
        this.pageBean = pageBean;
    }

    /**
     * @return
     * @throws JspException
     * @author cui
     * @createDate 2011-7-19
     */
    public void doTag() throws JspException, IOException {
        JspWriter out = this.getJspContext().getOut();
//		System.out.println(" pageTag pagebean is null:"+(pageBean==null));
        if (pageBean != null) {
            int currentPage = pageBean.getCurrentPage();
            int perpage = pageBean.getPerpage();
            int pageCount = pageBean.getPageCount();
//		int rowCount = pageBean.getRowCount();
            if (currentPage > pageCount) {
                currentPage = pageCount;
            }
            out.println("&nbsp;&nbsp;");
            if (currentPage <= 1) {
                out.println("首页");
                out.println("&nbsp;&nbsp;");
                out.println("上一页");
            } else {
                out.println("<a href=" + url + "currentPage=1&perpage=" + perpage + " title=首页>&laquo; 首页</a>");
                out.println("&nbsp;&nbsp;");
                out.println("<a href=" + url + "currentPage=" + (currentPage - 1 + "") + "&perpage=" + perpage + " title=上一页>上一页</a> ");
            }
            out.println("&nbsp;&nbsp;");
            if (currentPage >= pageCount) {
                if (pageCount > 4) {
                    out.println("...");
                    out.println("<a href=" + url + "currentPage=" + ((currentPage - 4) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage - 4) + ">" + (currentPage - 4) + "</a> ");
                    out.println("<a href=" + url + "currentPage=" + ((currentPage - 3) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage - 3) + ">" + (currentPage - 3) + "</a> ");
                    out.println("<a href=" + url + "currentPage=" + ((currentPage - 2) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage - 2) + ">" + (currentPage - 2) + "</a> ");
                    out.println("<a href=" + url + "currentPage=" + ((currentPage - 1) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage - 1) + ">" + (currentPage - 1) + "</a> ");
                    out.println("<a href=" + url + "currentPage=" + (currentPage + "&perpage=" + perpage + "") + " class=\"number current\" title=" + currentPage + ">" + currentPage + "</a> ");
                    out.println("下一页");
                    out.println("&nbsp;&nbsp;");
                    out.println("尾页");
                } else {
                    for (int i = 1; i <= pageCount; i++) {
                        if (i == currentPage) {
                            out.println("<a href=" + url + "currentPage=" + (i + "&perpage=" + perpage + "") + " class=\"number current\" title=" + i + ">" + i + "</a> ");
                        } else {
                            out.println("<a href=" + url + "currentPage=" + (i + "&perpage=" + perpage + "") + " class=\"number\" title=" + i + ">" + i + "</a> ");
                        }
                    }
                    out.println("下一页");
                    out.println("&nbsp;&nbsp;");
                    out.println("尾页");
                }
            } else {
                if (pageCount < 5) {
                    for (int i = 1; i <= pageCount; i++) {
                        if (i == currentPage) {
                            out.println("<a href=" + url + "currentPage=" + (i + "&perpage=" + perpage + "") + " class=\"number current\" title=" + i + ">" + i + "</a> ");
                        } else {
                            out.println("<a href=" + url + "currentPage=" + (i + "&perpage=" + perpage + "") + " class=\"number\" title=" + i + ">" + i + "</a> ");
                        }
                    }
                } else {
                    if (currentPage < 3) {
                        for (int i = 1; i <= 4; i++) {
                            if (i == currentPage) {
                                out.println("<a href=" + url + "currentPage=" + (i + "&perpage=" + perpage + "") + " class=\"number current\" title=" + i + ">" + i + "</a> ");
                            } else {
                                out.println("<a href=" + url + "currentPage=" + (i + "&perpage=" + perpage + "") + " class=\"number\" title=" + i + ">" + i + "</a> ");
                            }
                        }
                    } else if (currentPage < (pageCount - 2)) {
                        out.println("...");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage - 2) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage - 2) + ">" + (currentPage - 2) + "</a> ");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage - 1) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage - 1) + ">" + (currentPage - 1) + "</a> ");
                        out.println("<a href=" + url + "currentPage=" + (currentPage + "&perpage=" + perpage + "") + " class=\"number current\" title=" + currentPage + ">" + currentPage + "</a> ");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage + 1) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage + 1) + ">" + (currentPage + 1) + "</a> ");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage + 2) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage + 2) + ">" + (currentPage + 2) + "</a> ");
                        out.println("...");
                    } else if (currentPage == (pageCount - 2)) {
                        out.println("...");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage - 2) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage - 2) + ">" + (currentPage - 2) + "</a> ");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage - 1) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage - 1) + ">" + (currentPage - 1) + "</a> ");
                        out.println("<a href=" + url + "currentPage=" + (currentPage + "&perpage=" + perpage + "") + " class=\"number current\" title=" + currentPage + ">" + currentPage + "</a> ");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage + 1) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage + 1) + ">" + (currentPage + 1) + "</a> ");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage + 2) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage + 2) + ">" + (currentPage + 2) + "</a> ");
                    } else if (currentPage == (pageCount - 1)) {
                        out.println("...");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage - 3) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage - 3) + ">" + (currentPage - 3) + "</a> ");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage - 2) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage - 2) + ">" + (currentPage - 2) + "</a> ");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage - 1) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage - 1) + ">" + (currentPage - 1) + "</a> ");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage) + "&perpage=" + perpage + "") + " class=\"number current\" title=" + (currentPage) + ">" + (currentPage) + "</a> ");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage + 1) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage + 1) + ">" + (currentPage + 1) + "</a> ");
                    } else {
                        out.println("...");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage - 4) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage - 4) + ">" + (currentPage - 4) + "</a> ");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage - 3) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage - 3) + ">" + (currentPage - 3) + "</a> ");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage - 2) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage - 2) + ">" + (currentPage - 2) + "</a> ");
                        out.println("<a href=" + url + "currentPage=" + ((currentPage - 1) + "&perpage=" + perpage + "") + " class=\"number\" title=" + (currentPage - 1) + ">" + (currentPage - 1) + "</a> ");
                        out.println("<a href=" + url + "currentPage=" + (currentPage + "&perpage=" + perpage + "") + " class=\"number current\" title=" + currentPage + ">" + currentPage + "</a> ");
                    }
                }
                out.println("<a href=" + url + "currentPage=" + (currentPage + 1 + "") + "&perpage=" + perpage + " title=下一页>下一页 &raquo;</a>");
                out.println("&nbsp;&nbsp;");
                out.println("<a href=" + url + "currentPage=" + (pageCount + "") + "&perpage=" + perpage + " title=尾页>尾页</a>");
            }
            out.println("&nbsp;&nbsp;");
            out.println("共【" + pageBean.getRowCount() + "】条　显示第【 " + currentPage + "/" + pageCount + "】 页");
            out.println("&nbsp;&nbsp;");
            out.println("每页 ");
            out.println("<input type='text' maxlength=3 value=" + perpage + " " +
                    "style='font-size: 11;width: 30px;height:16px;' " +
                    "onblur='javaScript: var re = /^[1-9]+[0-9]*]*$/;" +
                    "if(re.test(this.value)){if(this.value>100){alert(\"最大值为100\");this.value=\"\";this.focus();return;}" +
                    "location.href=\"" + url + "perpage=\"+this.value;}" +
                    "else{alert(\"请输入整数（例如：3,5,6）!\");this.value=\"\";this.focus();}' />");
            out.println(" 条");
            out.println("跳到");
            String textid = "tagt" + tagnum;
//		System.out.println(textid);
            out
                    .println("<input type='text' id=" + textid
                            + "  maxlength=5  style='font-size: 11;width: 30px;height:16px; '/>");
            out.println("页");
            out
                    .println("<input type='button' value='跳到' onclick='javaScript:var re = /^[1-9]+[0-9]*]*$/;" +
                            "if(re.test(document.getElementById(\"" + textid + "\").value)){location.href=\""
                            + url
                            + "perpage="
                            + perpage
                            + "&currentPage=\"+document.getElementById(\"" + textid + "\").value;}" +
                            "else{alert(\"请输入整数（例如：3,5,6）!\");this.focus();return;}' />");
        }
    }

}
